/*
 * Copyright 2010 Nicolas Lochet Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 */
package org.nicocube.wol.dao.impl;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManagerFactory;

import org.nicocube.appengine.util.dao.Dao;
import org.nicocube.appengine.util.dao.DaoException;
import org.nicocube.appengine.util.dao.DaoQuery;
import org.nicocube.appengine.util.dao.DaoRequest;
import org.nicocube.appengine.util.dao.DaoResultContainer;
import org.nicocube.appengine.util.dao.NoResultFoundException;
import org.nicocube.appengine.util.dao.PersistenceAccessor;
import org.nicocube.gwt.util.client.gender.Gender;
import org.nicocube.gwt.util.client.ui.login.LoginFailedException;
import org.nicocube.wol.dao.UserDao;
import org.nicocube.wol.data.client.User;

import com.google.inject.Inject;

public class UserDaoImpl extends Dao implements UserDao {

	@Inject
	public UserDaoImpl(PersistenceManagerFactory pmf) {
		super(pmf);
	}

	@Override
	public User login(final String login, final String password)
	throws LoginFailedException {
		final DaoResultContainer<User> result = new DaoResultContainer<User>();
		try {
			eval(new DaoRequest() {
				@Override
				public void playOn(PersistenceAccessor pa) throws DaoException {
					DaoQuery<User> query = pa.newQuery(User.class);
					query.addCriteria("login", String.class, login);

					MessageDigest digest;
					try {
						digest = MessageDigest.getInstance("MD5");
					} catch (NoSuchAlgorithmException e) {
						throw new IllegalStateException("no MD5 implementation ?? No way !!");
					}

					digest.update(password.getBytes());
					String md5password = new String(digest.digest());
					
					query.addCriteria("password", String.class, md5password);

					result.set(query.execute());
				}
			});
		} catch (DaoException e) {
			if (e instanceof NoResultFoundException)
				throw new LoginFailedException();
		}
		return result.get();
	}

	@Override
	public User create(final String login, final String password, final String email, final Date dateOfBirth,
			final Gender gender, final String country, final String language, final boolean acceptNewsLetter,
			final boolean acceptPromotion, final boolean acceptPartner) {
		final DaoResultContainer<User> result = new DaoResultContainer<User>();
		eval(new DaoRequest(){
			@Override
			public void playOn(PersistenceAccessor pm) throws DaoException {
				MessageDigest digest;
				try {
					digest = MessageDigest.getInstance("MD5");
				} catch (NoSuchAlgorithmException e) {
					throw new IllegalStateException("no MD5 implementation ?? No way !!");
				}
				
				digest.update(password.getBytes());
				String md5password = new String(digest.digest());
				
				result.set(pm.makePersistent(new User(login, md5password, email, dateOfBirth, gender, country, language, acceptNewsLetter, acceptPromotion, acceptPartner)));
			}
		});
		return result.get();
	}

	@Override
	public List<User> getAllUser() {
		final List<User> result = new ArrayList<User>();
		eval(new DaoRequest() {
			@Override
			public void playOn(PersistenceAccessor pa) throws DaoException {
				DaoQuery<User> query = pa.newQuery(User.class).setOrdering("id");
				
				List<User> list = query.executeList();
				result.addAll(list);
			}
		});
		return result;
	}
	
}
